package com.anyou.mall.pla.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.pla.domain.VisibleUserTypeRelation;
import com.anyou.mall.pla.mapper.VisibleUserTypeRelationMapper;
import com.anyou.mall.pla.service.IVisibleUserTypeRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.pla.mapper.BannerMapper;
import com.anyou.mall.pla.domain.Banner;
import com.anyou.mall.pla.service.IBannerService;

/**
 * 轮播图管理Service业务层处理
 * 
 * @author gaochaohao
 * @date 2022-03-18
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper,Banner> implements IBannerService
{
    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private IBannerService iBannerService;

    @Autowired
    private VisibleUserTypeRelationMapper visibleUserTypeRelationMapper;

    @Autowired
    private IVisibleUserTypeRelationService iVisibleUserTypeRelationService;

    /**
     * 查询轮播图管理
     * 
     * @param bannerId 轮播图管理主键
     * @return 轮播图管理
     */
    @Override
    public Banner selectBannerByBannerId(Long bannerId)
    {
        Banner banner = bannerMapper.selectBannerByBannerId(bannerId);
        if (banner != null) {
            QueryWrapper<VisibleUserTypeRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("banner_id",bannerId);
            List<VisibleUserTypeRelation> visibleUserTypeRelationList = iVisibleUserTypeRelationService.list(wrapper);
            if (visibleUserTypeRelationList != null) {
               /* banner.setVisibleUserTypeList(visibleUserTypeRelationList);*/
            }
        }
        return banner;
    }

    /**
     * 查询轮播图管理列表
     * 
     * @param banner 轮播图管理
     * @return 轮播图管理
     */
    @Override
    public List<Banner> selectBannerList(Banner banner)
    {
        List<Banner> bannerList = bannerMapper.selectBannerList(banner);

        return bannerList;
    }

    /**
     * 新增轮播图管理
     * 
     * @param banner 轮播图管理
     * @return 结果
     */
    @Override
    public Boolean insertBanner(Banner banner)
    {
        banner.setCreateTime(DateUtils.getNowDate());
        banner.setDelFlag("0");
        boolean save = iBannerService.save(banner);
        /*if(save){
            if (banner.getVisibleUserTypeList() != null) {
                List<VisibleUserTypeRelation> visibleUserTypeList = banner.getVisibleUserTypeList();
                for (VisibleUserTypeRelation visibleUserTypeRelation : visibleUserTypeList) {
                    visibleUserTypeRelation.setBannerId(banner.getBannerId());
                    visibleUserTypeRelationMapper.insertVisibleUserTypeRelation(visibleUserTypeRelation);
                }
            }
        }*/
        return save;
    }

    /**
     * 修改轮播图管理
     * 
     * @param banner 轮播图管理
     * @return 结果
     */
    @Override
    public int updateBanner(Banner banner)
    {
        banner.setUpdateTime(DateUtils.getNowDate());
        // 更新关联表
        /*if (banner.getVisibleUserTypeList() != null) {
            iVisibleUserTypeRelationService.remove(new LambdaQueryWrapper<VisibleUserTypeRelation>().eq(VisibleUserTypeRelation::getBannerId,banner.getBannerId()));
            List<VisibleUserTypeRelation> visibleUserTypeList = banner.getVisibleUserTypeList();
            visibleUserTypeList.forEach(e -> {
                e.setBannerId(banner.getBannerId());
            });
            iVisibleUserTypeRelationService.saveBatch(visibleUserTypeList);
        }*/
        int update = bannerMapper.updateById(banner);
        return update;
    }

    /**
     * 批量删除轮播图管理
     * 
     * @param bannerIds 需要删除的轮播图管理主键
     * @return 结果
     */
    @Override
    public int deleteBannerByBannerIds(Long[] bannerIds)
    {
        /*if (bannerIds != null) {
            QueryWrapper<VisibleUserTypeRelation> wrapper = new QueryWrapper<>();
            wrapper.in("banner_id",bannerIds);
            visibleUserTypeRelationMapper.delete(wrapper);
        }*/
        return bannerMapper.deleteBannerByBannerIds(bannerIds);
    }

    /**
     * 删除轮播图管理信息
     * 
     * @param bannerId 轮播图管理主键
     * @return 结果
     */
    @Override
    public int deleteBannerByBannerId(Long bannerId)
    {
        if (bannerId != null) {
            QueryWrapper<VisibleUserTypeRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("banner_id",bannerId);
            visibleUserTypeRelationMapper.delete(wrapper);
        }
        return bannerMapper.deleteBannerByBannerId(bannerId);
    }

    @Override
    public ApiResult updateBannerStatus(Long bannerId) {
        Banner banner = bannerMapper.selectBannerByBannerId(bannerId);
        if (banner != null) {
            if ("1".equals(banner.getBannerStatus())) {
                banner.setBannerStatus("0");
            }else {
                banner.setBannerStatus("1");
            }
            return ApiResult.success(bannerMapper.updateBanner(banner));
        } else {
            return ApiResult.error("修改失败");
        }
    }
}
